package com.dormms.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dormms.bean.Dorm;
import com.dormms.bean.StuInfo;
import com.dormms.dto.StuDorm;
import com.dormms.dto.StuDormInfo;
import com.dormms.dto.UserRoleDto;
import com.dormms.service.DormService;
import com.dormms.service.StuInfoService;
import com.dormms.tool.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class DormController {
	@Autowired
	private DormService dormService;
	@Autowired
	private StuInfoService stuInfoService;
	
	@RequestMapping("admin/page.do")
	public String page(String pagename) {
		//对页面进行跳转
		pagename = "admin/" + pagename;
		return pagename;
	}
	
	@RequestMapping("admin/insertDorm")
	@ResponseBody
	public Integer insertDorm(Dorm dorm,HttpSession session) {
		UserRoleDto userRole = (UserRoleDto) session.getAttribute("userRoleDto");
		Integer dormLocationId = dormService.queryDormLocationIdByDormAdminId(userRole.getUserId());
		dorm.setDormLocationId(dormLocationId);
		dorm.setDormStatus(1);
		return dormService.insertDorm(dorm);
	}
	
	@RequestMapping("admin/insertStuDorm")
	@ResponseBody
	public Integer insertStuDorm(StuDorm stuDorm) {
		return dormService.insertStuDorm(stuDorm);
	}
	
	@RequestMapping("admin/updateDorm")
	@ResponseBody
	public Integer updateDorm(Dorm dorm) {
		return dormService.updateDorm(dorm);
	}
	
	@RequestMapping("admin/queryDormListByPage")
	@ResponseBody
	public PageInfo<Dorm> queryDormListByPage(Page page, Dorm dorm,HttpSession session) {
		UserRoleDto userRole = (UserRoleDto) session.getAttribute("userRoleDto");
		Integer dormLocationId = dormService.queryDormLocationIdByDormAdminId(userRole.getUserId());
		dorm.setDormLocationId(dormLocationId);
		PageHelper.startPage(page.getCurrPage(), page.getPageSize());
		List<Dorm> dorms = dormService.queryDormList(dorm);
		PageInfo<Dorm> pageInfo = new PageInfo<Dorm>(dorms);
		return pageInfo;
	}
	
	@RequestMapping("admin/queryAllDormNo")
	@ResponseBody
	public List<Dorm> queryAllDormNo(HttpSession session){
		UserRoleDto userRole = (UserRoleDto) session.getAttribute("userRoleDto");
		Integer dormLocationId = dormService.queryDormLocationIdByDormAdminId(userRole.getUserId());
		List<Dorm> dorms = dormService.queryAllDormNo(dormLocationId);
		return dorms;
	}
	
	@RequestMapping("admin/queryStuDormInfoByDormId")
	@ResponseBody
	public StuDormInfo queryStuDormInfoByDormId(Integer dormId) {
		return dormService.queryStuDormInfoByDormId(dormId);
	}
	
	@RequestMapping("admin/queryStuInfoByPage")
	@ResponseBody
	public PageInfo<StuInfo> queryStuInfoByPageByAdmin(StuInfo stuInfo, Page page, HttpSession session){
		UserRoleDto userRole = (UserRoleDto) session.getAttribute("userRoleDto");
		Integer dormLocationId = dormService.queryDormLocationIdByDormAdminId(userRole.getUserId());
		//对分页查询的的封装
		PageHelper.startPage(page.getCurrPage(), page.getPageSize());
		//根据学生信息查询
		List<StuInfo> stuInfos = stuInfoService.queryStuInfoByStuInfoByAdmin(stuInfo, dormLocationId);
		PageInfo<StuInfo> pageInfo = new PageInfo<StuInfo>(stuInfos);
		return pageInfo;
	}
	
	@RequestMapping(value="admin/queryStuInfoByStuId",method=RequestMethod.GET)
	@ResponseBody
	public StuInfo queryStuInfoByStuId(String stuId) {
		StuInfo stuInfo = stuInfoService.queryStuInfo(stuId);
		if("".equals(stuInfo.getStuId())) {
			return null;
		}
		return stuInfo;
	}
	
	@RequestMapping(value="admin/deleteStuDormInfo")
	@ResponseBody
	public Integer deleteStuDormInfo(String stuId, Integer dormId) {
		return dormService.deleteStuDormInfo(stuId,dormId);
	}
	
	@RequestMapping(value="admin/deleteDormInfo")
	@ResponseBody
	public Integer deleteDormInfo(Integer dormId) {
		return dormService.deleteDormInfo(dormId);
	}
	
	
}
